﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

public partial class admin_Contacts_List : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      ShowData();
    }
  }
  ContactBLL contactData = new ContactBLL();
  private void ShowData()
  {
    int sort = Convert.ToInt32(dropSort.SelectedValue);
    int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
    gridView.PageSize = PageRow;
    if (dropIsView.SelectedIndex == 0)
    {
      if (sort != -1)
        gridView.DataSource = contactData.GetAll(sort);
      else
        gridView.DataSource = contactData.GetAll();
    }
    else
    {
      bool isRead = false;
      if (dropIsView.SelectedValue == "2") isRead = true;

      if (sort != -1)
        gridView.DataSource = contactData.GetByRead(isRead, sort);
      else
        gridView.DataSource = contactData.GetAll(isRead);
    }
    gridView.DataBind();

  }
  public void ResetForm()
  {
    ltAddress.Text = "";
    ltContent.Text = "";
    ltDateSend.Text = "";
    ltEmail.Text = "";
    ltName.Text = "";
    ltPhone.Text = "";
    ltTitle.Text = "";
  }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    lbThongBao.Text = "";
    try
    {
      lbThongBao.Text = "";
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);

      contactData.Delete(id);
      ResetForm();
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[1].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[2].Text);
              contactData.Delete(id);             
            }
            break;
          }
        }
      }
      ResetForm();
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropIsView_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      bool isRead = Convert.ToBoolean(e.Row.Cells[6].Text);
      if (isRead) e.Row.Cells[6].Text = "Đã đọc";
      else
      {
        e.Row.Cells[6].Text = "Chưa đọc";
        e.Row.ForeColor = System.Drawing.Color.Red;
      }

      e.Row.Cells[5].Text = Convert.ToDateTime(e.Row.Cells[5].Text).ToShortDateString();
    }
    catch
    {

    }
  }
  protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int index = Convert.ToInt32(e.CommandArgument);
      int id = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
      Contact obj = contactData.GetById(id);
      ltAddress.Text = obj.Address;
      ltContent.Text = HttpUtility.HtmlDecode(obj.Content);
      ltDateSend.Text = obj.DateSend.ToString();
      ltEmail.Text = obj.Email;
      ltName.Text = obj.Name;
      ltPhone.Text = obj.Phone;
      ltTitle.Text = obj.Title;

      contactData.Read(id);
    }
    catch
    {

    }
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
    ShowData();
  }
}